package com.babyqiao.wallpaper.manager.controller;


import com.babyqiao.wallpaper.common.UnifyResult;
import com.babyqiao.wallpaper.manager.entity.WallpaperTag;
import com.babyqiao.wallpaper.manager.service.IWallpaperTagService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QLZ
 * @since 2021-02-20
 */
@RestController
@RequestMapping("/api/admin/tags")
@Api(tags = "专辑标签相关接口")
public class WallpaperTagController {
    @Autowired
    IWallpaperTagService wallpaperTagService;

    @PostMapping("tag")
    @ApiOperation(value = "添加标签",httpMethod = "POST")
    public UnifyResult addTag(@ApiParam(name = "tag",value = "标签",required = true)@RequestBody WallpaperTag tag){
        boolean save = wallpaperTagService.save(tag);
        return save? UnifyResult.success():UnifyResult.error();
    }


    @DeleteMapping("tag/{id}")
    @ApiOperation(value = "删除标签",httpMethod = "DELETE")
    public UnifyResult delTag(@ApiParam(name = "id",value = "标签id",required = true)@PathVariable String id){
        boolean remove=wallpaperTagService.delTagService(id);
        return remove?UnifyResult.success():UnifyResult.error();
    }

    @GetMapping("tag/{id}")
    @ApiOperation(value = "根据id查询标签",httpMethod = "GET")
    public UnifyResult getTag(@ApiParam(name = "id",value = "标签id",required = true) @PathVariable String id){
        WallpaperTag wallpaperTag = wallpaperTagService.getById(id);
        return UnifyResult.success().data("result",wallpaperTag);
    }

    @GetMapping("tags/{current}/{limit}")
    @ApiOperation(value = "分页查询所有标签",httpMethod = "GET")
    public UnifyResult getTagsByLimit(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable long current,
                                      @ApiParam(name = "limit", value = "每页查询条数", required = true) @PathVariable long limit){
        Page<WallpaperTag> page=new Page<>(current,limit);
        Page<WallpaperTag> wallpaperTagPage = wallpaperTagService.page(page, null);
        long total = page.getTotal();
        List<WallpaperTag> records = page.getRecords();

        return UnifyResult.success().data("result",records).data("total",total);
    }

    @GetMapping("tags")
    @ApiOperation(value = "查询所有标签",httpMethod = "GET")
    public UnifyResult getAllTags(){
        List<WallpaperTag> list = wallpaperTagService.list();
        return UnifyResult.success().data("result",list);

    }


}
